package com.koron.sinoprobe.web.purchase.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.ledger.bean.vo.SpProjectVO;
import com.koron.sinoprobe.web.ledger.service.SpProjectService;
import com.koron.sinoprobe.web.purchase.bean.po.SpPurchaseEstablish;
import com.koron.sinoprobe.web.purchase.bean.query.SpPurchaseEstablishQuery;
import com.koron.sinoprobe.web.purchase.bean.vo.SpPurchaseEstablishVO;
import com.koron.sinoprobe.web.purchase.mapper.SpPurchaseEstablishMapper;
import com.koron.sinoprobe.web.purchase.service.SpPurchaseEstablishService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 采购立项(SpPurchaseEstablish)表服务实现类
 *
 * @author jzp
 * @since 2021-12-29 10:19:39
 */
@Service
public class SpPurchaseEstablishServiceImpl implements SpPurchaseEstablishService {

    @Autowired
    private WorkFlowHandler workFlowHandler;
    @Autowired
    private SpProjectService spProjectService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public SpPurchaseEstablishVO getById(SessionFactory factory, String id) {
        SpPurchaseEstablishMapper mapper = factory.getMapper(SpPurchaseEstablishMapper.class);
        return mapper.selectById(id);
    }

    @Override
    @TaskAnnotation("getByProjectId")
    public SpPurchaseEstablish getByProjectId(SessionFactory factory, String projectId) {
        SpPurchaseEstablishMapper mapper = factory.getMapper(SpPurchaseEstablishMapper.class);
        return mapper.selectOneByProjectId(projectId);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<SpPurchaseEstablishVO> list(SessionFactory factory, SpPurchaseEstablishQuery query) {
        SpPurchaseEstablishMapper mapper = factory.getMapper(SpPurchaseEstablishMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<SpPurchaseEstablishVO> page(SessionFactory factory, PageQuery pageQuery, SpPurchaseEstablishQuery query) {
        OrderByInterceptor.setOrderBy(pageQuery.getOrders());            // 设置排序，若不设置排序，插件会因为没有排序字段而直接跳过，不会影响分页插件
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());  //设置分页
        List<SpPurchaseEstablishVO> spPurchaseEstablishList = list(factory, query);
        return new PageInfo<>(spPurchaseEstablishList);
    }

    @Override
    @TaskAnnotation("getRelated")
    public List<String> getRelated(SessionFactory factory) {
        SpPurchaseEstablishMapper mapper = factory.getMapper(SpPurchaseEstablishMapper.class);
        return mapper.selectRelated();
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public boolean saveOrUpdate(SessionFactory factory, SpPurchaseEstablish spPurchaseEstablish) {
        // 默认草稿
        if (StringUtils.isEmpty(spPurchaseEstablish.getStatus())) {
            spPurchaseEstablish.setStatus(Constants.AppceptStatus.DRAFT);
        }
        SpPurchaseEstablishMapper mapper = factory.getMapper(SpPurchaseEstablishMapper.class);
        return mapper.insertOrUpdate(spPurchaseEstablish) > 0;
    }

    @Override
    @TaskAnnotation("end")
    public boolean end(SessionFactory factory, String id) {
        // 1. 更新状态
        SpPurchaseEstablish spPurchaseEstablish = new SpPurchaseEstablish();
        spPurchaseEstablish.setId(id);
        spPurchaseEstablish.setStatus(Constants.AppceptStatus.APPROVED);
        spPurchaseEstablish.setFinishTime(new Date());
        update(factory, spPurchaseEstablish);
        SpPurchaseEstablishVO source = getById(factory, id);

        // 2. 立项完成后，项目的状态改为规划中，进度为1%
        SpProjectVO spProjectVO = spProjectService.selectOne(factory, source.getProjectId());
        spProjectVO.setProgress(1);
        spProjectVO.setMajor(source.getMajorCategory());
        spProjectService.saveOrUpdate(factory, spProjectVO);
        return true;
    }

    @Override
    @TaskAnnotation("submit")
    public boolean submit(SessionFactory factory, SpPurchaseEstablish spPurchaseEstablish) {
        // 2. 保存数据
        spPurchaseEstablish.setStatus(Constants.AppceptStatus.WAIT_REVIEW);
        saveOrUpdate(factory, spPurchaseEstablish);
        return true;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpPurchaseEstablish spPurchaseEstablish) {
        SpPurchaseEstablishMapper mapper = factory.getMapper(SpPurchaseEstablishMapper.class);
        return mapper.update(spPurchaseEstablish) > 0;
    }

    @Override
    @TaskAnnotation("updateStatus")
    public boolean updateStatus(SessionFactory factory, String id, String status) {
        SpPurchaseEstablish spPurchaseEstablish = new SpPurchaseEstablish();
        spPurchaseEstablish.setId(id);
        spPurchaseEstablish.setStatus(status);
        return update(factory, spPurchaseEstablish);
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        SpPurchaseEstablishMapper mapper = factory.getMapper(SpPurchaseEstablishMapper.class);
        return mapper.hidden(id) > 0;
    }
}
